package com.tonghe.controller;

import com.tonghe.domain.ResponseResult;
import com.tonghe.domain.dto.ArticleDto;
import com.tonghe.domain.dto.QueryArticleDTO;
import com.tonghe.service.ArticleService;
import com.tonghe.vo.sys.AddArticleDto;
import com.tonghe.vo.sys.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Date:2022/9/8- 09- 08- 15:16
 **/
@RestController
@RequestMapping("/content/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /** 写文章 */
    // TODO 我给了每个人都有写的权力，那是不是每个人都可以管理自己的文章呢 是的
    @PostMapping
    public ResponseResult add(@RequestBody AddArticleDto article){
        return articleService.add(article);
    }

    /** 列出文章 */
    @PreAuthorize("@ps.hasPermi('content:article:list')")
    @GetMapping("/list")
    public ResponseResult listArticle(Integer pageSize , Integer pageNum, QueryArticleDTO articleDTO) {
        return articleService.articleList(pageNum,pageSize,articleDTO);
    }

    @GetMapping(value = "/{id}")
    public ResponseResult getInfo(@PathVariable(value = "id")Long id){
        ArticleVo article = articleService.getInfo(id);
        return ResponseResult.okResult(article);
    }

    @PutMapping
    public ResponseResult edit(@RequestBody ArticleDto article){
        articleService.edit(article);
        return ResponseResult.okResult();
    }
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Long id){
        articleService.removeById(id);
        return ResponseResult.okResult();
    }

}
